package com.zwj.service.impl;

import com.zwj.bo.AddHomeworkBo;
import com.zwj.bo.SelectHomeworkInfoBo;
import com.zwj.bo.SelectUnApprovedBo;
import com.zwj.bo.WorkApproval;
import com.zwj.dto.AddHomeworkDto;
import com.zwj.dto.AddHomeworkSubmitDto;
import com.zwj.entity.UserEntity;

import com.zwj.enums.WorkTypeEnum;
import com.zwj.mapper.WorkInfoMapper;
import com.zwj.service.WorkInfoService;

import com.zwj.utils.DataCheckUtils;
import com.zwj.utils.DateTimeUtils;
import com.zwj.utils.DateUtils;
import com.zwj.vo.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.util.List;

/**
 * <p>
 * 作业信息表 服务实现类
 * </p>
 *
 * @author zwj
 * @since 2021-07-21
 */
@Service
public class WorkInfoServiceImpl implements WorkInfoService {
    @Autowired
    private WorkInfoMapper workInfoMapper;

    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
    public Result addHomework(AddHomeworkBo addHomeworkBo, UserEntity userEntity) {
        String title = addHomeworkBo.getTitle();
        String completeDate = addHomeworkBo.getCompleteDate();
        Long courseProjectInfoId = addHomeworkBo.getCourseProjectInfoId();
        String workType = addHomeworkBo.getWorkType();
        Long[] teamInfoId = addHomeworkBo.getTeamInfoId();
        Long[] studentId = addHomeworkBo.getStudentId();
        String appendsLink = addHomeworkBo.getAppendsLink();
        String remark = addHomeworkBo.getRemark();
        String name = userEntity.getId();
        String currentTime = DateTimeUtils.getCurrentTime();
        DataCheckUtils.isNotBlank(title,"作业主题不能为空");
        DataCheckUtils.isNotBlank(completeDate,"计划完成时间不能为空");
        DataCheckUtils.isNotValid(courseProjectInfoId == null ,"课程不能为空");
        DataCheckUtils.isNotBlank(workType,"作业类型不能为空");
        DataCheckUtils.isNotValid(WorkTypeEnum.getEnumValue(workType)==null,"作业类型错误");
        DataCheckUtils.isNotValid(teamInfoId == null,"项目小组不能为空");
        int compare = 0;
        try {
            compare = DateTimeUtils.compareToCurrent(completeDate, DateTimeUtils.YYYY_MM_DD_HH_MM_SS);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        DataCheckUtils.isValid(compare>0,"计划完成时间要大于当前时间");

        String teamInfoIds = StringUtils.join(teamInfoId, ",");
        String studentIds=null;
        if(studentId.length>0){
            studentIds = StringUtils.join(studentId,",");
        }

        if(workType.equals("2")||workType.equals("3")){
            studentIds=this.workInfoMapper.selectStudentIsLeader(teamInfoIds);
        }
        if(studentId.length==0){

            studentIds=this.workInfoMapper.selectAllStudent(teamInfoIds);
        }
        Integer selectFinalWork = this.workInfoMapper.selectFinalWork(courseProjectInfoId);
        if(workType.equals("3")){
            if(selectFinalWork>0){
                return Result.error("不能重复发布最终作业");
            }
        }
        if(selectFinalWork>0){
            return Result.error("该课程已经结束");
        }

        AddHomeworkDto addHomeworkDto = new AddHomeworkDto();
        addHomeworkDto.setAppendsLink(appendsLink);
        addHomeworkDto.setCourseProjectInfoId(courseProjectInfoId);
        addHomeworkDto.setTitle(title);
        addHomeworkDto.setWorkType(workType);
        addHomeworkDto.setCompleteDate(completeDate);
        addHomeworkDto.setCreateTime(currentTime);
        addHomeworkDto.setUpdateBy(name);
        addHomeworkDto.setUpdateTime(currentTime);
        addHomeworkDto.setCreateBy(name);
        addHomeworkDto.setRemark(remark);

        workInfoMapper.addHomework(addHomeworkDto);

        Long workId = addHomeworkDto.getWorkId();//获得自增的id

        String[] split = studentIds.split(",");
        for (int i = 0; i < split.length; i++) {
            AddHomeworkSubmitDto submitDto = new AddHomeworkSubmitDto();
            submitDto.setCreateBy(name);
            submitDto.setCreateTime(currentTime);
            submitDto.setUpdateBy(name);
            submitDto.setUpdateTime(currentTime);
            submitDto.setWorkId(workId);
            submitDto.setStudentId(split[i]);
            workInfoMapper.addHomeworkSubmit(submitDto);
        }
        return Result.success();
    }

    @Override
    public Result selectHomeWorkById(Long workId) {
        if (workId == null){
            return Result.error("作业编号不能为空");
        }
        SelectHomeworkVo selectHomeworkVo = workInfoMapper.selectHomeWorkById(workId);
        return selectHomeworkVo != null?Result.success(selectHomeworkVo):Result.error("没有查到信息");
    }

    @Override
    public Result selectUnApprovedHomework(SelectUnApprovedBo selectUnApprovedBo) {
        String workType = selectUnApprovedBo.getWorkType();
        if (workType != null && workType !=""){
            if (WorkTypeEnum.getEnumValue(workType) == null){
                return Result.error("作业类型错误");
            }
        }
        List<SelectUnApprovedVo> selectUnApprovedVos = workInfoMapper.selectUnApprovedHomework(selectUnApprovedBo);

        return selectUnApprovedVos != null && selectUnApprovedVos.size()>0?Result.success(selectUnApprovedVos):Result.error("没有数据");
    }

    @Override
    public Result selectHomeworkInfo(SelectHomeworkInfoBo selectHomeworkInfoBo){
        String workType = selectHomeworkInfoBo.getWorkType();
        if (workType != null) {
            DataCheckUtils.isValid(WorkTypeEnum.getEnumValue(workType) != null, "作业类型不存在");
        }
        List<SelectHomeworkInfoVo> selectHomeworkInfoVos = workInfoMapper.selectHomeworkInfo(selectHomeworkInfoBo);
        for (SelectHomeworkInfoVo selectHomeworkInfoVo : selectHomeworkInfoVos) {
            Long workId = selectHomeworkInfoVo.getWorkId();
            selectHomeworkInfoVo.setDaiTiJiao(workInfoMapper.selectSumByState(workId,"0"));
            selectHomeworkInfoVo.setDaiPiFu(workInfoMapper.selectSumByState(workId,"1"));
            selectHomeworkInfoVo.setYiPiFu(workInfoMapper.selectSumByState(workId,"2"));
        }
        return selectHomeworkInfoVos != null && selectHomeworkInfoVos.size()>0?Result.success(selectHomeworkInfoVos):Result.error("没有数据");
    }

    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
    public Result deleteHomework(Long workId) {
        int j = workInfoMapper.deleteHomework(workId);
        int i = workInfoMapper.deleteHomeworkSubmit(workId);
        return i>0 &&j>0?Result.success(): Result.error("删除失败");

    }

    @Override
    public Result selectApprovedHomework(SelectUnApprovedBo selectUnApprovedBo) {
        String workType = selectUnApprovedBo.getWorkType();
        if (workType != null && workType !=""){
            if (WorkTypeEnum.getEnumValue(workType) == null){
                return Result.error("作业类型错误");
            }
        }
        List<SelectUnApprovedVo> selectUnApprovedVos = workInfoMapper.selectApprovedHomework(selectUnApprovedBo);
        return selectUnApprovedVos != null && selectUnApprovedVos.size()>0?Result.success(selectUnApprovedVos):Result.error("没有数据");
    }

    @Override
    public Result selectHomeworkStudent(Long submitId, UserEntity userEntity) {
        DataCheckUtils.isNotValid(submitId==null,"作业提交id不能为空");
        SelectHomeworkStudentVo selectHomeworkStudentVo=workInfoMapper.selectHomeworkStudent(submitId);
        return Result.success(selectHomeworkStudentVo);
    }

    @Override
    public Result submitWorkApproval(WorkApproval workApproval, UserEntity userEntity) {
        Float score=workApproval.getScore();
        DataCheckUtils.isNotValid(workApproval.getSubmitId()==null,"作业提交id不能为空");
        DataCheckUtils.isNotValid(score==null,"得分不能为空");
        DataCheckUtils.isValid(score>=0&&score<=100,"得分为0到100的数");
        Integer submitWorkApproval=workInfoMapper.submitWorkApproval(workApproval);
        if(submitWorkApproval>0){
            return Result.success("批复成功");
        }
        return Result.error("批复失败");
    }

    @Override
    public Result saveWorkApproval(WorkApproval workApproval, UserEntity userEntity) {
        Float score=workApproval.getScore();
        DataCheckUtils.isNotValid(workApproval.getSubmitId()==null,"作业提交id不能为空");
        DataCheckUtils.isNotValid(score==null,"得分不能为空");
        DataCheckUtils.isValid(score>=0&&score<=100,"得分为0到100的数");
        SelectHomeworkStudentVo selectHomeworkStudentVo=workInfoMapper.selectHomeworkStudent(workApproval.getSubmitId());
        if(selectHomeworkStudentVo.getWorkStatus()==2){
            return Result.error("该作业已批复");
        }
        Integer saveWorkApproval=workInfoMapper.saveWorkApproval(workApproval);
        if(saveWorkApproval>0){
            return Result.success("保存成功");
        }
        return Result.error("保存失败");
    }
}
